package www.cskaoyan.com.utils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * 手动把一些Java常见的数据封装成json数据
 */
public class SplicJson {

    // 把data数据转为json数据拼接到buffer中
    // data说明:  1,允许是数组  2,允许对象 3,允许是collection对象 4,允许是map对象
    public static void  appJson(Object data,  StringBuffer buffer){
        if (data == null)return;
        // 递归实现

        if (data instanceof Map){
            // 是Map对象: 处理map
            buffer.append("[");
            appJsonMap(data, buffer);
            buffer.append("]");
        }else if (data instanceof Collection){
            // 是Collection对象: 处理Collection
            buffer.append("[");
            appJsonCollection(data, buffer);
            buffer.append("]");
        }else if (data instanceof String){
            // 是String对象: 处理String
            buffer.append("\""+data+"\"");
        }else if (data instanceof Character){
            // 是Character对象: 处理Character
            buffer.append("\""+data+"\"");
        }else if (data instanceof Integer){
            // 是Integer对象: 处理Integer
            buffer.append( data );
        }else if (data instanceof Float){
            buffer.append( data );
        }else if (data instanceof Double){
            buffer.append( data );
        }else if (data instanceof Short){
            buffer.append( data );
        }else if (data instanceof Byte){
            buffer.append( data );
        }else if (data instanceof Long){
            buffer.append( data );
        }else if (data instanceof Boolean){
            buffer.append( data);
        }else {
            try{
                Object[] objs = (Object[]) data;
                // 是数组: 处理数组
                buffer.append("[");
                appJsonArray(data, buffer);
                buffer.append("]");
            }catch (Exception e){
                // 是一个对象: 处理对象
                buffer.append("{");
                appJsonObj(data, buffer);
                buffer.append("}");
            }
        }
    }

    private static void appJsonObj(Object data, StringBuffer buffer) {
        //可以使用getDeclaredFields()方法获取对象的所有属性
        Field[] fields = data.getClass().getDeclaredFields();	// 获取对象的所有属性
        for (Field item : fields) {
            String parmName = item.getName();	// 获取对象属性名
            buffer.append("\""+ parmName +"\":");
            try {
                item.setAccessible(true);
                Object o = item.get(data);
                appJson( o  , buffer);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            buffer.append(",");
        }
        buffer.replace(buffer.length()-1, buffer.length(), "");
    }
    private static void appJsonArray(Object data, StringBuffer buffer) {
        Object[] objs = (Object[]) data;

        for (int i = 0; i < objs.length; i++) {
            if (objs[i] == null)continue;
            appJson(objs[i] , buffer);
            buffer.append("," );
        }
        buffer.delete(buffer.length()-1, buffer.length());
    }
    private static void appJsonCollection(Object data, StringBuffer buffer) {
        Collection<Object> collection = (Collection) data;

        for (Object item : collection) {
            appJson(item , buffer);
            buffer.append("," );
        }
        buffer.delete(buffer.length()-1, buffer.length());
    }
    private static void appJsonMap(Object data, StringBuffer buffer) {
        Map<String, Object> map = (Map) data;
        Set<String> strings = map.keySet();

        for (String key : strings) {
            buffer.append("\"" + key + "\":" );
            appJson(map.get(key), buffer);
            buffer.append("," );
        }
        buffer.delete(buffer.length()-1, buffer.length());
    }

}
